package org.jeecg.modules.icms.invoice.service.impl;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.icms.invoice.dto.IcmsInvoiceDetailDto;
import org.jeecg.modules.icms.invoice.entity.IcmsInvoiceTitle;
import org.jeecg.modules.icms.invoice.mapper.IcmsInvoiceTitleMapper;
import org.jeecg.modules.icms.invoice.service.IIcmsInvoiceTitleService;
import org.jeecg.modules.icms.util.HandleExcelInfo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 发票抬头
 * @Author: jeecg-boot
 * @Date:   2024-01-10
 * @Version: V1.0
 */
@Service
public class IcmsInvoiceTitleServiceImpl extends ServiceImpl<IcmsInvoiceTitleMapper, IcmsInvoiceTitle> implements IIcmsInvoiceTitleService {

    @Resource
    private IcmsInvoiceTitleMapper icmsInvoiceTitleMapper;

    @Resource
    private HandleExcelInfo handleExcelInfo;
    @Override
    public IcmsInvoiceTitle addTitleInfo(String titleInfo, Sheet sheet) throws Exception{
        String regex = "销货单位[:：](.*)\\s+地\\s*址[:：](.*?)\\s+税号[:：](.*)开户银行[:：](.*)\\s+帐号[:：](.*)购货单位[:：](.*)地\\s*址[:：](.*)\\s+税号[:：](.*)开户银行[:：](.*)\\s+帐号[:：](.*)";
        Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
        IcmsInvoiceTitle icmsInvoiceTitle = new IcmsInvoiceTitle();

        Matcher matcher = pattern.matcher(titleInfo);
        // 查找匹配 匹配不到提示用户title格式错误 停止后续操作 不再解析
        if (matcher.find()) {
            // 提取信息
            icmsInvoiceTitle.setSaleCompany(matcher.group(1).trim());
            icmsInvoiceTitle.setSaleAddr(matcher.group(2).trim());
            icmsInvoiceTitle.setSaleTaxNo(matcher.group(3).trim());
            icmsInvoiceTitle.setSaleBank(matcher.group(4).trim());
            icmsInvoiceTitle.setSaleBankCard(matcher.group(5).trim());
            icmsInvoiceTitle.setBuyCompany(matcher.group(6).trim());
            icmsInvoiceTitle.setBuyAddr(matcher.group(7).trim());
            icmsInvoiceTitle.setBuyTaxNo(matcher.group(8).trim());
            icmsInvoiceTitle.setBuyBank(matcher.group(9).trim());
            icmsInvoiceTitle.setBuyBankCard(matcher.group(10).trim());
        }
        if (handleExcelInfo.checkObjAllFieldsIsNull(icmsInvoiceTitle))
            throw new JeecgBootException(sheet.getSheetName()+"表中:开票单抬头格式错误，请按照模板填写!");
        return icmsInvoiceTitle;
    }
}
